package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/19 11:10
 * @Version 1.0
 */
@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("list.html")
    public String list(SearchParam searchParam,Model model){
        //  后台要存 searchParam urlParam trademarkParam propsParamList orderMap
        //  查询urlParam -- 作用 记录当前用户根据什么条件进行查询的！
        //  trademarkList attrsList goodsList pageNo totalPages 这些是 SearchResponseVo
        String urlParam = this.makeUrlParam(searchParam);
        //  面包屑制作：
        String trademarkParam = this.makeTrademarkParam(searchParam.getTrademark());
        //  平台属性面包屑：
        List<SearchAttr> propsParamList = this.makePropsList(searchParam.getProps());
        //  创建排序对象
        Map orderMap = this.makeOrderMap(searchParam.getOrder());
        //  远程调用
        Result<Map> result = listFeignClient.list(searchParam);
        //  result.getData()==SearchResponseVo addAllAttributes(Map); map 与实体类可以互换
        model.addAllAttributes(result.getData());
        model.addAttribute("searchParam",searchParam);
        model.addAttribute("urlParam",urlParam);
        model.addAttribute("trademarkParam",trademarkParam);
        model.addAttribute("propsParamList",propsParamList);
        model.addAttribute("orderMap",orderMap);


        //  返回检索页面
        return "list/index";
    }

    /**
     * 设置排序规则
     * @param order
     * @return
     */
    private Map makeOrderMap(String order) {
        //  创建一个map 对象
        Map map = new HashMap();
        //  判断排序规则不为空
        if (!StringUtils.isEmpty(order)){
            //  取排序规则order=1:asc  order=1:desc  order=2:asc  order=2:desc
            //  分割获取数据
            String[] split = order.split(":");
            if (null != split && split.length==2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        } else {
            map.put("type","1");
            map.put("sort","desc");
        }
        //  返回排序规则
        return map;
    }

    /**
     * 平台属性面包屑
     * @param props
     * @return
     */
    private List<SearchAttr> makePropsList(String[] props) {
        //  什么一个集合
        List<SearchAttr> searchAttrList = new ArrayList<>();
        //  判断
        if (null != props && props.length>0){
            for (String prop : props) {
                //  平台属性名:平台属性值名 props=24:128G:机身内存
                String[] split = prop.split(":");
                if (null != split && split.length ==3){
                    //  声明一个对象
                    SearchAttr searchAttr = new SearchAttr();
                    searchAttr.setAttrId(Long.parseLong(split[0]));
                    searchAttr.setAttrValue(split[1]);
                    searchAttr.setAttrName(split[2]);
                    searchAttrList.add(searchAttr);
                }
            }
        }
        return searchAttrList;
    }

    /**
     * 品牌面包屑
     * @param trademark
     * @return
     */
    private String makeTrademarkParam(String trademark) {
        //  判断是根据品牌进行了过滤 trademark=2:苹果
        if (!StringUtils.isEmpty(trademark)){
            //  分割：
            String[] split = trademark.split(":");
            if (null != split && split.length ==2){
                return "品牌:"+split[1];
            }
        }
        return "";
    }

    /**
     * 记录用户查询条件方法
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        //  创建对象
        StringBuffer stringBuffer = new StringBuffer();

        //  通过判断这个对象中的属性是否为空！
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            //  http://list.gmall.com/list.html?category3Id=61
            stringBuffer.append("category3Id=").append(searchParam.getCategory3Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            //  http://list.gmall.com/list.html?category2Id=13
            stringBuffer.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            //  http://list.gmall.com/list.html?category1Id=2
            stringBuffer.append("category1Id=").append(searchParam.getCategory1Id());
        }
        //  判断用户是否根据关键词检索.
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            //  http://list.gmall.com/list.html?keyword=小米
            stringBuffer.append("keyword=").append(searchParam.getKeyword());
        }
        //  判断用户是否根据品牌过滤.
        if (!StringUtils.isEmpty(searchParam.getTrademark())){
                //  http://list.gmall.com/list.html?category3Id=61&trademark=1:小米
                if (stringBuffer.length()>0){
                    stringBuffer.append("&trademark=").append(searchParam.getTrademark());
                }
        }
        //  判断用户是否根据平台属性检索
        //  http://list.gmall.com/list.html?category3Id=61&trademark=1:小米&props=66:15.6:屏幕尺寸
        String[] props = searchParam.getProps();
        if (null != props && props.length>0){
            for (String prop : props) {
                if (stringBuffer.length()>0){
                    stringBuffer.append("&props=").append(prop);
                }
            }
        }
        //  返回url。
        return "list.html?"+stringBuffer.toString();
    }
}
